package com.lsk.admin.AdminSystem;

import com.jfinal.aop.Before;
import com.jfinal.aop.Duang;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.jfinal.plugin.redis.Cache;
import com.jfinal.plugin.redis.Redis;
import com.jfinal.weixin.sdk.api.ApiResult;
import com.jfinal.weixin.sdk.api.QrcodeApi;
import com.lsk.util.PageUtil;
import com.lsk.util.ReturnData;
import com.lsk.util.Syslog;
import com.lsk.util.Tools;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONObject;

public class AdminSystemController extends Controller {
    static AdminSystemService adminSystemService = (AdminSystemService)Duang.duang(AdminSystemService.class);

    Cache cache = Redis.use("ACTIVITYUSER");

    public void roleList() {
        String currentPage = getPara("currentPage");
        String size = getPara("size");
        Map<String, String[]> map = getParaMap();
        String userId = getSession().getAttribute("userId").toString();
        ReturnData data = adminSystemService.roleList(map, userId);
        PageUtil<List<Record>> menu = (PageUtil<List<Record>>)data.getData();
        setAttr("menu", menu);
        setAttr("currentPage", currentPage);
        setAttr("size", size);
        renderJsp("adminRole.jsp");
    }

    public void addRole() {
        String userId = getSession().getAttribute("userId").toString();
        renderJsp("role-add.jsp");
    }

    public void givePermission() {
        String userId = getSession().getAttribute("userId").toString();
        String permissionId = getPara("permissionId");
        List<Record> oneList = adminSystemService.getPermissionByUserIdAndPermissionId(userId, permissionId);
        String type = getPara("type");
        String id = getPara("id");
        List<Record> twoList = new ArrayList<>();
        if ("role".equals(type)) {
            twoList = adminSystemService.selectPermissionByRoleId(id);
        } else if ("user".equals(type)) {
            twoList = adminSystemService.selectPermissionByUserId(id);
        }
        setAttr("oneList", oneList);
        setAttr("twoList", twoList);
        setAttr("type", type);
        setAttr("id", id);
        setAttr("permissionId", permissionId);
        renderJsp("permissionList.jsp");
    }

    @Before({Tx.class})
    public void saveRoleOrUserPermission() {
        Map<String, String[]> map = getParaMap();
        ReturnData result = adminSystemService.saveRoleOrUserPermissionService(map);
        renderJson(result);
    }

    public void permissionTree() {
        String type = getPara("type");
        String id = getPara("id");
        String userId = getSession().getAttribute("userId").toString();
        List<Record> oneList = adminSystemService.treePermissionByUserId(userId);
        List<Record> twoList = null;
        if ("role".equals(type)) {
            twoList = adminSystemService.selectPermissionByRoleId(id);
        } else if ("user".equals(type)) {
            twoList = adminSystemService.selectPermissionByUserId(id);
        }
        setAttr("oneList", oneList);
        setAttr("twoList", twoList);
        setAttr("type", type);
        setAttr("id", id);
        renderJsp("permissionList1.jsp");
    }

    public void onePermission() {
        String type = getPara("type");
        String id = getPara("id");
        String userId = getSession().getAttribute("userId").toString();
        List<Record> oneList = adminSystemService.onePermissionService(userId);
        List<Record> roleOneList = null;
        List<Record> userOneList = null;
        if ("role".equals(type)) {
            roleOneList = adminSystemService.selectPermissionByRoleId(id);
        } else if ("user".equals(type)) {
            userOneList = adminSystemService.selectPermissionByUserId(id);
        }
        setAttr("roleOneList", roleOneList);
        setAttr("userOneList", userOneList);
        setAttr("oneList", oneList);
        setAttr("type", type);
        setAttr("id", id);
        renderJsp("permissionList1.jsp");
    }

    public void twoPermission() {
        String type = getPara("type");
        String id = getPara("id");
        String permissionId = getPara("onePermissionId");
        String userId = getSession().getAttribute("userId").toString();
        List<Record> twoList = adminSystemService.twoPermissionService(permissionId, userId);
        List<Record> roleOneList = null;
        List<Record> userOneList = null;
        if ("role".equals(type)) {
            roleOneList = adminSystemService.selectPermissionByRoleId(id);
        } else if ("user".equals(type)) {
            userOneList = adminSystemService.selectPermissionByUserId(id);
        }
        setAttr("roleOneList", roleOneList);
        setAttr("userOneList", userOneList);
        setAttr("twoList", twoList);
        setAttr("type", type);
        setAttr("id", id);
        renderJsp("permissionList2.jsp");
    }

    public void threePermission() {
        String type = getPara("type");
        String id = getPara("id");
        String permissionId = getPara("twoPermissionId");
        String userId = getSession().getAttribute("userId").toString();
        List<Record> threeList = adminSystemService.threePermissionService(permissionId, userId);
        List<Record> roleOneList = null;
        List<Record> userOneList = null;
        if ("role".equals(type)) {
            roleOneList = adminSystemService.selectPermissionByRoleId(id);
        } else if ("user".equals(type)) {
            userOneList = adminSystemService.selectPermissionByUserId(id);
        }
        setAttr("roleOneList", roleOneList);
        setAttr("userOneList", userOneList);
        setAttr("threeList", threeList);
        setAttr("type", type);
        setAttr("id", id);
        renderJsp("permissionList3.jsp");
    }

    public void settingUserOrRolePerssion() {
        String id = getPara("id");
        String type = getPara("type");
        String permissinId = getPara("permissinId");
        boolean state = getParaToBoolean("state").booleanValue();
        adminSystemService.settingUserOrRolePerssionService(id, type, permissinId, state);
    }

    @Before({Tx.class})
    public void addRoleInfo() throws Exception {
        String userId = getSession().getAttribute("userId").toString();
        AdminSystemService adminSystemService = (AdminSystemService)enhance(AdminSystemService.class);
        Map<String, String[]> map = getParaMap();
        ReturnData data = adminSystemService.addRoleInfo(map, userId);
        renderJson(data);
    }

    @Before({Tx.class})
    public void roleDel() throws Exception {
        String userId = getSession().getAttribute("userId").toString();
        Map<String, String[]> map = getParaMap();
        AdminSystemService adminSystemService = (AdminSystemService)enhance(AdminSystemService.class);
        ReturnData data = adminSystemService.roleDel(map, userId);
        renderJson(data);
    }

    public void findRoleInfo() {
        String rid = getPara("rid");
        String userId = getSession().getAttribute("userId").toString();
        ReturnData data = adminSystemService.findRoleInfo(rid, userId);
        setAttr("data", data);
        setAttr("rid", rid);
        renderJsp("role-update.jsp");
    }

    public void updateRole() {
        Map<String, String[]> map = getParaMap();
        ReturnData data = adminSystemService.updateRole(map);
        renderJson(data);
    }

    public void adminRuleList() {
        String currentPage = getPara("currentPage");
        String limit = getPara("limit");
        String type = getPara("type");
        Map<String, String[]> map = getParaMap();
        ReturnData returnData = adminSystemService.ruleList(map);
        PageUtil<List<Record>> menu = (PageUtil<List<Record>>)returnData.getData();
        ReturnData topReturnData = adminSystemService.topMenu();
        List<Record> topMenu = (List<Record>)topReturnData.getData();
        setAttr("topMenu", topMenu);
        setAttr("menu", menu);
        setAttr("currentPage", currentPage);
        setAttr("limit", limit);
        setAttr("type", type);
        renderJsp("adminRule.jsp");
    }

    public void savePermission() {
        String id = getPara("id");
        String url = getPara("url");
        String parentid = getPara("parentid");
        String name = getPara("name");
        ReturnData returnData = adminSystemService.savePermissionService(id, parentid, url, name);
        renderJson(returnData);
    }

    public void delPermission() {
        String id = getPara("id");
        Record record = (new Record()).set("id", id);
        boolean del = Db.delete("sys_permission", "id", record);
        renderJson(Boolean.valueOf(del));
    }

    public void editPermission() {
        String id = getPara("id");
        ReturnData returnData = adminSystemService.topMenu();
        Record record = Db.findFirst("SELECT *from sys_permission where id='" + id + "'");
        setAttr("rec", record);
        setAttr("top", returnData.getData());
        setAttr("id", id);
        renderJsp("adminRuleEdit.jsp");
    }

    public void permissionType() {
        ReturnData returnData = adminSystemService.rpermissionTypeService(getParaMap());
        Object oo = returnData.getData();
        setAttr("returnData", oo);
        renderJsp("admin-cate.jsp");
    }

    public void saveRpermissionType() {
        ReturnData returnData = adminSystemService.saveRpermissionTypeService(getParaMap());
        renderJson(returnData);
    }

    public void toRpermissionType() {
        String id = getPara("id");
        String sql = "SELECT *from sys_permission_type where id='" + id + "'";
        Record record = Db.findFirst(sql);
        setAttr("record", record);
        renderJsp("cate-edit.jsp");
    }

    public void test() {
        renderJsp("test.jsp");
    }

    public void department() {
        ReturnData returnData = adminSystemService.topDepartment();
        ReturnData all = adminSystemService.department();
        setAttr("topDepartment", returnData);
        setAttr("all", all);
        renderJsp("department.jsp");
    }

    public void departmentEdit() {
        String id = getPara("id");
        ReturnData all = adminSystemService.department();
        String sql = "SELECT *from sys_department where id='" + id + "'";
        Record record = Db.findFirst(sql);
        setAttr("all", all.getData());
        setAttr("rec", record);
        renderJsp("departmentEdit.jsp");
    }

    public void saveDepartment() {
        String id = getPara("id");
        String name = getPara("name");
        String userId = String.valueOf(getSession().getAttribute("userId"));
        ReturnData returnData = adminSystemService.saveDepartment(id, name, userId);
        renderJson(returnData);
    }

    public void delPartment() {
        String id = getPara("id");
        Record record = (new Record()).set("id", id);
        boolean sa = Db.delete("sys_department", "id", record);
        renderJson(Boolean.valueOf(sa));
    }

    public void adminList() {
        int currentPage = getParaToInt("currentPage").intValue();
        int size = getParaToInt("size").intValue();
        String userName = getPara("userName");
        String userCode = getPara("userCode");
        String userId = getSession().getAttribute("userId").toString();
        String userType = getSession().getAttribute("userType").toString();
        String type = getPara("type");
        List<Record> list = adminSystemService.adminListService(currentPage, size, userName, userCode, userType);
        setAttr("list", list);
        setAttr("type", type);
        setAttr("userName", userName);
        setAttr("userCode", userCode);
        setAttr("currentPage", Integer.valueOf(currentPage));
        renderJsp("adminList.jsp");
    }

    public void goEditUserRole() {
        String id = getPara("id");
        String userId = getSession().getAttribute("userId").toString();
        List<Record> myList = adminSystemService.getRolesByUserId(userId);
        List<Record> userList = adminSystemService.getRolesByUserId(id);
        setAttr("myList", myList);
        setAttr("userList", userList);
        setAttr("id", id);
        renderJsp("editUserRole.jsp");
    }

    public void saveUserRole() throws Exception {
        String userId = getSession().getAttribute("userId").toString();
        Map<String, String[]> map = getParaMap();
        ReturnData returnData = adminSystemService.saveUserRoleService(map, userId);
        renderJson(returnData);
    }

    public void goAdminEdit() {
        String userId = getSession().getAttribute("userId").toString();
        Record userRecord = (Record)this.cache.get(userId);
        String id = getPara("id");
        String type = getPara("type");
        String sql = "SELECT * from sys_user where id='" + id + "'";
        Record record = Db.findFirst(sql);
        String sql1 = "SELECT\n\tsr.id,\n\tsr.`name`\nFROM\n\tsys_role sr\nLEFT JOIN sys_user_role sur ON sr.id = sur.sys_user_id\nWHERE\n\tsur.sys_user_id = '" + id + "'";
        List<Record> onRole = Db.find(sql1);
        List<Record> roleList = (List<Record>)userRecord.get("role");
        setAttr("rec", record);
        setAttr("id", id);
        setAttr("type", type);
        setAttr("roleList", roleList);
        setAttr("onRole", onRole);
        renderJsp("adminEdit.jsp");
    }

    @Before({Tx.class})
    public void saveAdmin() {
        String userId = getSession().getAttribute("userId").toString();
        Map<String, String[]> map = getParaMap();
        ReturnData returnData = adminSystemService.saveAdminService(map, userId);
        renderJson(returnData);
    }

    public void getAdmin() {
        Map<String, String[]> map = getParaMap();
        ReturnData returnData = adminSystemService.getAdmin(map);
        renderJson(returnData);
    }

    public void delAdmin() {
        String id = getPara("id");
        Record record = (new Record()).set("id", id);
        boolean del = Db.delete("sys_user", "id", record);
        renderJson(Boolean.valueOf(del));
    }

    public void saveAuthorization() {
        Map<String, String[]> map = getParaMap();
        ReturnData returnData = adminSystemService.saveUserPermission(map);
        renderJson(returnData);
    }

    public void departmentRoleList() {
        String id = getPara("id");
        Map map = adminSystemService.departmentRoleListService(id);
        renderJson(map);
    }

    public void buttonAjax() {
        String id = getPara("id");
        ReturnData returnData = adminSystemService.buttonAjaxService(id);
        setAttr("returnData", returnData);
        renderJsp("permissionAjax.jsp");
    }

    public void authorizationButton() {
        String id = getPara("id");
        String sql = "SELECT *from sys_permission where parentid='" + id + "' and type='button'";
        List<Record> recordList = Db.find(sql);
        setAttr("parentid", id);
        setAttr("rec", recordList);
        renderJsp("ButtonPermission.jsp");
    }

    public void editAuthorizationButton() {
        String id = getPara("id");
        String parentid = getPara("parentid");
        String sql = "SELECT *from sys_permission where id='" + id + "'";
        Record record = Db.findFirst(sql);
        setAttr("rec", record);
        setAttr("parentid", parentid);
        renderJsp("ButtonPermissionEdit.jsp");
    }

    public void saveAuthorizationButton() {
        String parentid = getPara("parentid");
        String id = getPara("id");
        String name = getPara("name");
        String url = getPara("url");
        ReturnData returnData = adminSystemService.saveAuthorizationButtonService(id, name, url, parentid);
        renderJson(returnData);
    }

    public void authorization() {
        String type = getPara("type");
        String authorizationUserId = getPara("authorizationUserId");
        String userId = String.valueOf(getSession().getAttribute("userId"));
        ReturnData data = adminSystemService.findAllResource(type, userId);
        ReturnData authorizationUser = adminSystemService.userPermissionByUserId(authorizationUserId);
        setAttr("data", data);
        setAttr("type", type);
        setAttr("authorizationUser", authorizationUser);
        setAttr("authorizationUserId", authorizationUserId);
        renderJsp("permission.jsp");
    }

    public void callBackImages() {
        Map<Object, Object> map = new HashMap<>();
        map.put("code", Integer.valueOf(0));
        JSONObject json = JSONObject.fromObject(map);
        renderJson(json.toString());
    }

    public void goOrgNumberEdit() {
        String id = getPara("id");
        ReturnData returnData = adminSystemService.goOrgNumberEditService(id);
        setAttr("data", returnData.getData());
        setAttr("id", id);
        renderJsp("orgNumber.jsp");
    }

    public void addOrgNumber() {
        String id = getPara("id");
        int addNumber = getParaToInt("addNumber").intValue();
        ReturnData returnData = adminSystemService.addOrgNumberService(id, addNumber);
        renderJson(returnData);
    }

    public void bindingWeiXin() {
        String userId = getSession().getAttribute("userId").toString();
        Map<String, String> map = new HashMap<>();
        int sceneId = Tools.getRandomNum() + Tools.getRandomNum();
        map.put("userId", userId);
        map.put("type", "bindingWeiXin");
        this.cache.setex(Integer.valueOf(sceneId), 4200, map);
        ApiResult result = QrcodeApi.createTemporary(3600, sceneId);
        Syslog.info(result.getJson());
        String imgPath = "";
        try {
            imgPath = QrcodeApi.getShowQrcodeUrl(result.getStr("ticket"));
        } catch (Exception e) {
            Syslog.error(e);
        }
        setAttr("imgPath", imgPath);
        renderJsp("bindingWeiXin.jsp");
    }

    public void uploaderweima() {
        renderJsp("uploadAdminerweima.jsp");
    }

    public void adminActivityList() {
        String activityName = getPara("activityName");
        String orgName = getPara("orgName");
        String personTel = getPara("personTel");
        setAttr("activityName", activityName);
        setAttr("orgName", orgName);
        setAttr("personTel", personTel);
        renderJsp("adminActivityList.jsp");
    }

    public void adminActivityListDate() {
        Map<String, String[]> map = getParaMap();
        Map resultMap = adminSystemService.adminActivityList(map);
        renderJson(resultMap);
    }

    public void adminSetTalkMoney() {
        String id = getPara("id");
        String talkMoney = getPara("talkMoney");
        setAttr("id", id);
        setAttr("talkMoney", talkMoney);
        renderJsp("adminSetTalkMoney.jsp");
    }

    public void saveTalkMoney() {
        int talkMoney = getParaToInt("talkMoney").intValue() * 100;
        String id = getPara("id");
        ReturnData returnData = new ReturnData();
        String sql = "UPDATE yx_activity set talkMoney=" + talkMoney + " where id=" + id;
        int up = Db.update(sql);
        renderText(up + "");
    }

    public void systemConfig() {
        Record systemConfig = adminSystemService.systemConfigService();
        setAttr("systemConfig", systemConfig);
        renderJsp("systemConfig.jsp");
    }

    public void saveSystemConfig() {
        Map<String, String[]> map = getParaMap();
        ReturnData returnData = adminSystemService.saveSystemConfigService(map);
        renderJson(returnData);
    }

    public void welcome() {
        String userId = getSession().getAttribute("userId").toString();
        Map map = adminSystemService.welcomeService(userId);
        setAttr("statistics", map);
        renderJsp("../weclome.jsp");
    }
}
